﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using HYElectricKernel.Generics;

namespace HYElectricKernel
{
	public class Common
	{
		public static List<DimensionType> GetDimensionTypes(Autodesk.Revit.DB.Document doc)
		{
			List<DimensionType> list = new List<DimensionType>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(DimensionType)).ToElements())
			{
				DimensionType item = element as DimensionType;
				list.Add(item);
			}
			return list;
		}

		public static List<WallType> GetWallTypes(Autodesk.Revit.DB.Document doc)
		{
			List<WallType> list = new List<WallType>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(WallType)).ToElements())
			{
				WallType item = element as WallType;
				list.Add(item);
			}
			return list;
		}

		public static List<TextNoteType> GetTextNoteTypes(Autodesk.Revit.DB.Document doc)
		{
			List<TextNoteType> list = new List<TextNoteType>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(TextNoteType)))
			{
				TextNoteType item = element as TextNoteType;
				list.Add(item);
			}
			return list;
		}

		public static double MMToFeet(double val)
		{
			return val / 304.8;
		}

		public static double FeetToMM(double val)
		{
			return val * 304.8;
		}

		public static void get3dViewName(Autodesk.Revit.DB.Document doc, View3D view3d)
		{
			List<int> list = new List<int>();
			FilteredElementIterator elementIterator = new FilteredElementCollector(doc).OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				Autodesk.Revit.DB.View view = element as Autodesk.Revit.DB.View;
				if (view != null && !view.IsTemplate && (int)view.ViewType == 4)
				{
					string name = view.Name;
					string text = "局部三维视图";
					int num = name.LastIndexOf(text);
					if (num > -1)
					{
						int item = Convert.ToInt32(name.Substring(num + text.Length));
						list.Add(item);
					}
				}
			}
			list.Sort();
			int num2 = 0;
			if (list.Count > 0)
			{
				num2 = list[list.Count - 1];
			}
			view3d.Name = "局部三维视图" + (num2 + 1).ToString();
		}

		public static void UpdateModel(UIDocument uiDoc, bool autoJoin)
		{
			uiDoc.Document.Regenerate();
			if (autoJoin)
			{
				uiDoc.Document.AutoJoinElements();
			}
			uiDoc.RefreshActiveView();
		}

		public static List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(Level)).ToElements())
			{
				Level level = element as Level;
				double elevation = level.Elevation;
				if (Geometry.Greaterthan_Or_Equal(elevation, bottom) && Geometry.Lessthan_Or_Equal(elevation, top))
				{
					list.Add(level);
				}
			}
			list.Sort(new LevelComparer());
			return list;
		}

		public static List<XYZ> UniqueAndSortIntersectPoints(Curve curve, List<XYZ> intersectPoints)
		{
			Arc arc = curve as Arc;
			if (null != arc)
			{
				XYZ hyendPoint = arc.GetEndPoint(0);
				if (arc.Normal.Z < 0.0)
				{
					hyendPoint = arc.GetEndPoint(1);
				}
				intersectPoints.Sort(new AngleComparer(hyendPoint, arc.Center));
			}
			Line line = curve as Line;
			if (null != line)
			{
				XYZ hyendPoint2 = line.GetEndPoint(0);
				intersectPoints.Sort(new DistanceComparer(hyendPoint2));
			}
			IEnumerable<XYZ> enumerable = intersectPoints.Distinct(new Comparint<XYZ>((XYZ x, XYZ y) => x != null && y != null && x.DistanceTo(y) < 0.001));
			List<XYZ> list = new List<XYZ>();
			foreach (XYZ item in enumerable)
			{
				list.Add(item);
			}
			return list;
		}

		public static int GetInt(double a)
		{
			if (a == 0.0)
			{
				return 0;
			}
			double num;
			if (a > 0.0)
			{
				num = a + 0.99999999999999989;
			}
			else
			{
				num = a + -0.999999999999999;
			}
			return (int)num;
		}

		public static int GetTextByteLength(string str)
		{
			return Encoding.Default.GetBytes(str).Length;
		}

		public static bool IsValidating(string numStr, double minRange, double maxRange)
		{
			try
			{
				double val = Convert.ToDouble(numStr);
				if (Geometry.LessThan(val, minRange))
				{
					return false;
				}
				if (Geometry.GreaterThan(val, maxRange))
				{
					return false;
				}
			}
			catch (Exception)
			{
				return false;
			}
			return true;
		}

		public static int Rounding(double value)
		{
			return (int)(value * 10.0 + 0.5) / 10;
		}

		private const double _eps = 1E-09;
	}
}
